
rm(list = ls())

library(tidyverse)
library(readxl)
library(ggplot2) 
library(ggthemes)


###Figure 1###

YouGovClean <- read_csv("YouGovClean.csv")

se_fxn <- function(x) {sd(x)/sqrt(length(x))}
lower_bound <- function(x) {mean(x) - qnorm(.975)*(sd(x)/sqrt(length(x)))}
upper_bound <- function(x) {mean(x) + qnorm(.975)*(sd(x)/sqrt(length(x)))}


sell_congress2016 <- YouGovClean %>% select(contains("sell")) %>% select(-contains("pres"))
sell_pres2016 <- YouGovClean %>% select(ends_with("pres")) 

price <- c(0, 25, 50, 100, 200, 500, 1000)
nonzero <- c("0","1","1","1","1","1","1")

means_congress2016 <- c(0, apply(sell_congress2016, 2, function(x) mean(x)))
se_congress2016 <- c(0, apply(sell_congress2016, 2, function(x) se_fxn(x)))
lb_congress2016 <- c(0, apply(sell_congress2016, 2, function(x) lower_bound(x)))
ub_congress2016 <- c(0, apply(sell_congress2016, 2, function(x) upper_bound(x)))

means_pres2016 <- c(0, apply(sell_pres2016, 2, function(x) mean(x)))
se_pres2016 <- c(0, apply(sell_pres2016, 2, function(x) se_fxn(x)))
lb_pres2016 <- c(0, apply(sell_pres2016, 2, function(x) lower_bound(x)))
ub_pres2016 <- c(0, apply(sell_pres2016, 2, function(x) upper_bound(x)))

  
congress2016_df <- data.frame(price, means_congress2016, se_congress2016, lb_congress2016, ub_congress2016, nonzero)
congress2016_df        

pres2016_df <- data.frame(price, means_pres2016, se_pres2016, lb_pres2016, ub_pres2016, nonzero)
pres2016_df   

# 2016 Congress Plot
ggplot(congress2016_df, aes(x = means_congress2016, y = price)) +
  geom_line(size=.7) +
  geom_errorbarh(aes(xmin = means_congress2016 -1.96*se_congress2016, xmax = means_congress2016 + 1.96*se_congress2016), size=.5, color="black")+
  geom_point(data = subset(congress2016_df, nonzero == "1"), shape = 21, fill = "lightblue",
             color = "black", size = 2) +
  theme_classic(base_size = 9) + scale_x_continuous(breaks = seq(0, .4, 0.1), labels = paste0(seq(0, .4, 0.1)*100, " %"), expand = c(0, 0)) + coord_cartesian(xlim=c(0.0001,.45)) +
  scale_y_continuous(breaks = seq(0, 1030, 200),expand = c(0, 50), limits = c(0, 1030)) +
  labs(title = "US Congressional Candidate (2016 Survey)", x = "% of Respondents Willing to Accept Offer in Exchange for Vote", y = "Offer ($)") + theme(plot.title = element_text(hjust = 0.5))

# 2016 Presidential Plot
ggplot(pres2016_df, aes(x = means_pres2016, y = price)) +
  geom_line(size=.7) +
  geom_errorbarh(aes(xmin = means_pres2016 -1.96*se_pres2016, xmax = means_pres2016 + 1.96*se_pres2016), size=.5, color="black")+
  geom_point(data = subset(pres2016_df, nonzero == "1"), shape = 21, fill = "lightblue",
             color = "black", size = 2) +
  theme_classic(base_size = 9) + scale_x_continuous(breaks = seq(0, .4, 0.1), labels = paste0(seq(0, .4, 0.1)*100, " %"), expand = c(0, 0)) + coord_cartesian(xlim=c(0.0001,.45)) +
  scale_y_continuous(breaks = seq(0, 1030, 200),expand = c(0, 50), limits = c(0, 1030)) +
  labs(title = "US Presidential Candidate (2016 Survey)", x = "% of Respondents Willing to Accept Offer in Exchange for Vote", y = "Offer ($)") + theme(plot.title = element_text(hjust = 0.5))



###Figure 2###

rm(list = ls())

MTurkClean <- read_csv("MTurkClean.csv")

se_fxn <- function(x) {sd(x)/sqrt(length(x))}


sell_congress2018_top <- MTurkClean %>% filter(dem_impt == 10) %>% 
                                    select(contains("sell")) %>% 
                                    select(-contains("pres")) 

sell_congress2018_bottom <- MTurkClean %>% filter(dem_impt < 10) %>% 
                                    select(contains("sell")) %>% 
                                    select(-contains("pres")) 
                                     
sell_pres2018_top <- MTurkClean %>% filter(dem_impt == 10) %>% 
                                    select(ends_with("pres")) 

sell_pres2018_bottom <- MTurkClean %>% filter(dem_impt < 10) %>% 
                                    select(ends_with("pres")) 

price <- c(0, 25, 50, 100, 200, 500, 1000)
nonzero <- c("0","1","1","1","1","1","1")

means_congress2018_top <- c(0, apply(sell_congress2018_top, 2, function(x) mean(x)))
means_congress2018_bottom <- c(0, apply(sell_congress2018_bottom, 2, function(x) mean(x)))
se_congress2018_top <- c(0, apply(sell_congress2018_top, 2, function(x) se_fxn(x)))
se_congress2018_bottom <- c(0, apply(sell_congress2018_bottom, 2, function(x) se_fxn(x)))

means_pres2018_top <- c(0, apply(sell_pres2018_top, 2, function(x) mean(x)))
means_pres2018_bottom <- c(0, apply(sell_pres2018_bottom, 2, function(x) mean(x)))

se_pres2018_top <- c(0, apply(sell_pres2018_top, 2, function(x) se_fxn(x)))
se_pres2018_bottom <- c(0, apply(sell_pres2018_bottom, 2, function(x) se_fxn(x)))

congress2018_top_df <- data.frame(price, means_congress2018_top, nonzero, se_congress2018_top) 
congress2018_bottom_df <- data.frame(price, means_congress2018_bottom, nonzero, se_congress2018_bottom) 

pres2018_top_df <- data.frame(price, means_pres2018_top, nonzero, se_pres2018_top) 
pres2018_bottom_df <- data.frame(price, means_pres2018_bottom, nonzero, se_pres2018_bottom) 

#need the x-axis variable to have the same name for both the above median and below median subsets
congress2018_top_df <- congress2018_top_df %>% rename(means = means_congress2018_top)
congress2018_bottom_df <- congress2018_bottom_df %>% rename(means = means_congress2018_bottom)
pres2018_top_df <- pres2018_top_df %>% rename(means = means_pres2018_top)
pres2018_bottom_df <- pres2018_bottom_df %>% rename(means = means_pres2018_bottom)


#2018 Congress Plot
 ggplot(NULL, aes(x = means, y= price)) +
  geom_line(data = congress2018_top_df, size=.5) +
  geom_errorbarh(data = congress2018_top_df, 
                 aes(xmin = means_congress2018_top - 1.96*se_congress2018_top, xmax = means_congress2018_top + 1.96*se_congress2018_top),
                 size=.3, color="black") +
  geom_point(data = subset(congress2018_top_df, nonzero == "1"), shape = 21, fill = "lightblue",
             color = "black", size = 2) +
  theme_classic(base_size = 9) + scale_x_continuous(breaks = seq(0,.6,0.1), labels = paste0(seq(0,.6,0.1)*100," %"),expand = c(0, 0)) + coord_cartesian(xlim=c(0.00001,.58)) +
  scale_y_continuous(breaks = seq(0, 1100, 200),expand = c(0, 50), limits = c(0, 1100)) +
  labs(title = "Willingness to Accept Vote-Buying Offer, \nCongressional Candidate (2018 Survey)", x = "% of Respondents Willing to Accept Offer in Exchange for Vote", y = "Offer ($)") + theme(plot.title = element_text(hjust = 0.5)) +
  geom_line(data = congress2018_bottom_df, size=.5) +
  geom_errorbarh(data = congress2018_bottom_df, 
                 aes(xmin = means_congress2018_bottom - 1.96*se_congress2018_bottom, xmax = means_congress2018_bottom + 1.96*se_congress2018_bottom), 
                 size=.3, color="black") +
  geom_point(data = subset(congress2018_bottom_df, nonzero == "1"), shape = 21, fill = "red",
             color = "red", size = 2) +
  annotate(geom = "text", x = .511, y = 1070, label = "Below Median",
           color = "black", size = 3, fontface = "italic") +
  annotate(geom = "text", x = .312, y = 1070, label = "Above Median",
           color = "black", size = 3, fontface = "italic")
 
 
 #2018 Presidential Plot
 ggplot(NULL, aes(x = means, y= price)) +
   geom_line(data = pres2018_top_df, size=.5) +
   geom_errorbarh(data = pres2018_top_df, 
                  aes(xmin = means_pres2018_top - 1.96*se_pres2018_top, xmax = means_pres2018_top + 1.96*se_pres2018_top),
                  size=.3, color="black") +
   geom_point(data = subset(pres2018_top_df, nonzero == "1"), shape = 21, fill = "lightblue",
              color = "black", size = 2) +
   theme_classic(base_size = 9) + scale_x_continuous(breaks = seq(0,.6,0.1), labels = paste0(seq(0,.6,0.1)*100," %"),expand = c(0, 0)) + coord_cartesian(xlim=c(0.00001,.58)) +
   scale_y_continuous(breaks = seq(0, 1100, 200),expand = c(0, 50), limits = c(0, 1100)) +
   labs(title = "Willingness to Accept Vote-Buying Offer, \nPresidential Candidate (2018 Survey)", x = "% of Respondents Willing to Accept Offer in Exchange for Vote", y = "Offer ($)") + theme(plot.title = element_text(hjust = 0.5)) +
   geom_line(data = pres2018_bottom_df, size=.5) +
   geom_errorbarh(data = pres2018_bottom_df, 
                  aes(xmin = means_pres2018_bottom - 1.96*se_pres2018_bottom, xmax = means_pres2018_bottom + 1.96*se_pres2018_bottom), 
                  size=.3, color="black") +
   geom_point(data = subset(pres2018_bottom_df, nonzero == "1"), shape = 21, fill = "red",
              color = "red", size = 2) +
   annotate(geom = "text", x = .511, y = 1070, label = "Below Median",
            color = "black", size = 3, fontface = "italic") +
   annotate(geom = "text", x = .312, y = 1070, label = "Above Median",
            color = "black", size = 3, fontface = "italic")